<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm"  :inline="true" v-show="showSearch">
            <el-form-item label="字典名称" prop="dictName">
                <el-input v-model="queryParams.dictName" placeholder="请输入字典名称" clearable style="width: 240px"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="字典编码" prop="dictType">
                <el-input v-model="queryParams.dictType" placeholder="请输入字典编码" clearable style="width: 240px"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择" clearable style="width: 240px">
                    <el-option v-for="dict in dict.type.SYS_NORMAL_DISABLE" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8" ref="refbut">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus"  @click="handleAdd">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit"  :disabled="single" @click="handleUpdate">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete"  :disabled="multiple" @click="handleDelete">删除</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="warning" plain icon="el-icon-download"  @click="handleExport">导出</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" plain icon="el-icon-refresh"  @click="handleRefreshCache">刷新缓存</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" border :data="dictList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="序号" type="index" :index="table_index" prop="dictId" align="center"  />
            <el-table-column label="字典名称" prop="dictName" align="center" :show-overflow-tooltip="true"  />
            <el-table-column label="字典编码" prop="dictType" align="center" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <router-link :to="'/tenantManage/templateManage/dictManage-data/index/' + scope.row.dictId" class="link-type">
                  <span>{{ scope.row.dictType }}</span>
                </router-link>
              </template>
            </el-table-column>
            <el-table-column label="字典描述" prop="remark" align="center" :show-overflow-tooltip="true" />
            <el-table-column label="状态" prop="status" align="center" >
              <template slot-scope="scope">
                <dict-tag :options="dict.type.SYS_NORMAL_DISABLE" :value="scope.row.status"/>
              </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改字典配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="45%" style="margin-top:8%" append-to-body>
            <el-form ref="dictForm" :model="dictForm" :rules="rules" label-width="120px">
                <el-row>
                  <el-col :span="24">
                    <el-form-item label="字典名称" prop="dictName">
                      <el-input v-model="dictForm.dictName" placeholder="请输入字典名称" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="字典编码" prop="dictType">
                    <el-input v-model="dictForm.dictType" placeholder="请输入字典编码" />
                  </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="字典描述" prop="remark">
                    <el-input v-model="dictForm.remark" type="textarea" placeholder="请输入字典描述" />
                  </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="状态" prop="status">
                      <el-radio-group v-model="dictForm.status">
                        <el-radio
                          v-for="dict in dict.type.SYS_NORMAL_DISABLE"
                          :key="dict.value"
                          :label="dict.value"
                        >{{dict.label}}</el-radio>
                      </el-radio-group>
                  </el-form-item>
                  </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listDictData, getDictType, addDict, updateDict, delDictType, refreshCache } from '@/api/manage/dict/dictManage';
import estimateTableHeight from '@/mixins/estimateTableHeight';
//vue项目中element-tabel自动计算高度
export default {
    name: "Role",
    user_Dicts: ['SYS_NORMAL_DISABLE'],
    mixins: [estimateTableHeight], // 混入方法
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            label: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 字典表格数据
            dictList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 是否显示弹出层（数据权限）
            openDataScope: false,
            menuExpand: false,
            menuNodeAll: false,
            deptExpand: true,
            deptNodeAll: false,
            // 日期范围
            dateRange: [],
            // 菜单列表
            menuOptions: [],
            // 部门列表
            deptOptions: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                dictName: undefined,
                dictType: undefined,
                status: undefined
            },
            // 表单参数
            dictForm: {},
            // 选择是否系统字典
            systemFlagOption: [
              {value: 'Y',label: '是'},
              {value: 'N',label: '否'}
            ],
            // 选择状态
            statusOption: [
              {value: 'ENABLE',label: '启用'},
              {value: 'DISABLE',label: '关闭'}
            ],
            // 表单校验
            rules: {
                dictName: [
                    { required: true, message: "字典名称不能为空", trigger: "blur" }
                ],
                dictType: [
                    { required: true, message: "字典编码不能为空", trigger: "blur" }
                ],
                // status: [
                //     { required: true, message: "状态不能为空", trigger: "blur" }
                // ]
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        table_index(index){
          return (this.queryParams.pageNum-1) * this.queryParams.pageSize + index + 1;
        },
        /** 查询字典列表 */
        getList() {
            this.loading = true;
            listDictData(this.queryParams).then(response => {
                this.dictList = response.rows;
                this.total = response.total;
                this.loading = false;
              }
            );
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.dictForm = {
                dictId: undefined,
                dictName: undefined,
                dictType: undefined,
                remark: undefined,
                status: undefined
            };
            this.resetForm("dictForm");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = [];
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.dictId)
            this.single = selection.length != 1
            this.multiple = !selection.length
            this.label = selection.map(item => item.dictName)
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加字典类型";
            this.dictForm.status = '0'
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const dictId = row.dictId || this.ids
            getDictType(dictId).then(response => {
                this.dictForm = response.data;
                this.open = true;
                this.title = "修改字典类型";
            });
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["dictForm"].validate(valid => {
                if (valid) {
                    if (this.dictForm.dictId != undefined) {
                        updateDict(this.dictForm).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addDict(this.dictForm).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
          const dictIds = row.dictId || this.ids;
          const label = row.dictName || this.label
          this.$modal.confirm('是否确认删除字典名称为"' + label + '"的数据项？').then(function() {
            return delDictType(dictIds);
          }).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          }).catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
          this.download('/manage/yqdaDict/type/export', {
            ...this.queryParams
          }, `type_${new Date().getTime()}.xlsx`)
        },
        /** 刷新缓存按钮操作 */
        handleRefreshCache() {
          refreshCache().then(() => {
            this.$modal.msgSuccess("刷新成功");
          });
        }
    }
};
</script>
